package main;
import editor.LevelEditor;
import game.Game;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;

import levelSelect.LevelSelect;
import networking.SMap;

public class Main extends JPanel implements ActionListener
{
	private JFrame frame = new JFrame("Snake");
	private JPanel centerPanel = new JPanel();
	private JPanel menuPanel = new JPanel();
	private JMenuBar menuBar = new JMenuBar();
	private JMenu fileMenu = new JMenu("File");
	private JMenuItem exitMenuItem = new JMenuItem("Exit");
	
	public static void main(String[] args)
	{
		new Main();
	}
	
	public Main()
	{
		this.setLayout(new BorderLayout());
		
		centerPanel = new Splash(this);
		
		menuBar.add(fileMenu);
		fileMenu.add(exitMenuItem);
		exitMenuItem.addActionListener(this);
		
		this.add(menuBar, BorderLayout.NORTH);
		
		this.add(centerPanel, BorderLayout.CENTER);
		
		frame.setUndecorated(true);
		frame.setVisible(true);
		frame.setFocusable(true);
		frame.setLocation(50, 50);
		frame.setSize(601,700);
		frame.add(this);
		frame.validate();
		//JOptionPane.showMessageDialog(null, "Welcome to Snake");
		//backToMainMenu();
	}
	
	public void mainMenuChooseItem(int item)
	{
		frame.remove(centerPanel);
		if(item == 0)
		{
			centerPanel = new Game(frame, this);
			centerPanel.setFocusable(true);
			this.add(centerPanel, BorderLayout.CENTER);
		}
		else if(item == 1)
		{
			centerPanel = new LevelEditor(50, 50, this);
			centerPanel.setFocusable(true);
			this.add(centerPanel, BorderLayout.CENTER);
		}
		else if(item == 2)
		{
			centerPanel = new LevelSelect(this, true);
			centerPanel.setFocusable(true);
			this.add(centerPanel, BorderLayout.CENTER);
		}
		else if(item == 3)
		{
			centerPanel = new LevelSelect(this, false);
			centerPanel.setFocusable(true);
			this.add(centerPanel, BorderLayout.CENTER);
		}
		/*else if(item == 4)
		{
			panel = new HighScore();
			frame.add(panel);
		}*/
		frame.setSize(centerPanel.getWidth(), centerPanel.getHeight() + 25);
		centerPanel.grabFocus();
	}
	
	public void backToMainMenu()
	{
		frame.remove(centerPanel);
		centerPanel = null;
		centerPanel = new MainMenu(this);
		centerPanel.setFocusable(true);
		
		centerPanel.validate();
		
		//frame.removeAll();
		
		frame.add(centerPanel);
		centerPanel.grabFocus();
		frame.validate();
	}

	public void mainMenuChooseLevel(int i) 
	{
		frame.remove(centerPanel);
		centerPanel = new Game(frame, this, i);
		centerPanel.setFocusable(true);
		frame.add(centerPanel);
		centerPanel.grabFocus();
		frame.validate();
	}

	public void testMap(SMap testMap) 
	{
		frame.remove(centerPanel);
		centerPanel = new Game(frame, this, testMap);
		centerPanel.validate();
		centerPanel.setFocusable(true);
		frame.add(centerPanel);
		centerPanel.grabFocus();
		frame.validate();
		
	}

	public void backToEditor(SMap cMAP) 
	{
		frame.remove(centerPanel);
		centerPanel = new LevelEditor(50, 50, this, cMAP);
		centerPanel.setFocusable(true);
		frame.add(centerPanel);
		centerPanel.grabFocus();
		frame.setSize(centerPanel.getWidth(), centerPanel.getHeight());
		frame.validate();
	}

	public void mainMenuPickLevel(SMap map)
	{
		frame.remove(centerPanel);
		centerPanel = new Game(frame, this, map);
		centerPanel.setFocusable(true);
		frame.add(centerPanel);
		centerPanel.grabFocus();
		frame.validate();
		
	}

	@Override
	public void actionPerformed(ActionEvent e)
	{
		if(e.getSource() == exitMenuItem)
		{
			System.exit(0);
		}
		
	}

}